<template>
    <el-dialog :title="coushareid==-1?'新增营销活动':'修改营销活动'" :visible.sync="open" :before-close="close" append-to-body>
        <CouLists @getcousid="getcousid"/>
        <el-form :model="f" ref="CouponForm">
            <el-form-item label="营销名称" :label-width="formLabelWidth">
                <el-input v-model="f.marketingName" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="营销时间" :label-width="formLabelWidth">
                <el-date-picker
                    v-model="useTimeValue"
                    type="datetimerange"
                    placeholder="选择时间范围">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="营销类型" :label-width="formLabelWidth">
                <el-button type="primary">分享赠券</el-button>
            </el-form-item>
            <el-form-item label="指定优惠券" :label-width="formLabelWidth">
                <el-input class="inputw" v-model="couponId" auto-complete="off" disabled></el-input>
                <el-button @click="getgood()">选择指定优惠券</el-button>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close()">取 消</el-button>
            <el-button type="primary" @click="save()">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import CouLists from 'components/Coupon/CouLists'
import moment from 'moment'
export default {
    data(){
        return {
            open:false,
            formLabelWidth:'120px',
            f:{},
            coushareid:-1,
            useTimeValue:[],
            couponId:''
        }
    },
    components:{
        CouLists
    },
    computed:{
        getstate(){
            return this.$store.state.coushareclass
        },
        getchange(){
            return this.$store.state.coushare
        }
    },
    watch:{
        getstate(v){//打开弹窗
            this.open=v
        },
        getchange(v){//获取入参填写表格
            if(v.marketingId){
                this.f.marketingName=v.marketingName
                this.useTimeValue=[new Date(v.beginTime),new Date(v.endTime)]
                this.coushareid=v.marketingId
                this.couponId=v.couponId
            }
        }
    },
    methods:{
        close(){//关闭弹窗并清空数据
            this.f={}
            this.coushareid=-1
            this.useTimeValue=[]
            this.couponId=''
            this.$store.state.coushareclass=false
            this.$store.state.coushare=false
        },
        getgood(){//新增
            this.$store.commit('ChangeCouLists')
        },
        save(){//保存
            var _this=this
            var pp=new URLSearchParams()
            pp.append('marketingName',this.f.marketingName)
            pp.append('type',1)
            pp.append('beginTime',moment(this.useTimeValue[0]).format("YYYY-MM-DD HH:mm:ss"))
            pp.append('endTime',moment(this.useTimeValue[1]).format("YYYY-MM-DD HH:mm:ss"))
            pp.append('couponId',this.couponId)
            if(this.coushareid==-1){//新增
                axios.get('/saveMarketing.do',{params:pp}).then(r=>{
                    if(r.data.code==200){
                        _this.$message.success("新增成功!")
                        _this.$store.state.coushare={}
                        _this.$store.state.cousharedialog=false
                        _this.close()
                    }else{
                        _this.$message.error(r.data.message)
                    }
                })
            }else{
                pp.append('coushareid ',this.coushareid)
                axios.get('/updateMarketing.do',{params:pp}).then(r=>{
                    if(r.data.code==200){
                        _this.$message.success("修改成功!")
                        _this.$store.state.coushare={}
                        _this.$store.state.cousharedialog=false
                        _this.close()
                    }else{
                        _this.$message.error(r.data.message)
                    }
                })
            }
        },
        getcousid(m){//获取优惠券id
            this.couponId=null
            this.couponId=m
        }
    }
}
</script>
<style scoped>
.inputw{
    width: 100px; 
    margin-right: 10px;
}
.el-dialog__header {
    padding: 10px;
    background-color: #20a0ff!important;
}
</style>
